#!/bin/sh

# 06.10.2005 Volker Quetschke
# Test for parallel operation.
# (no issue, sanity check only)

: ${DMAKEPROG:=dmake}
file1="mymakefile.mk"
file2="testfile"
file3="testfile1"
file4="testfile2"
file5="testfile3"
tmpfiles="$file1 $file2 $file3 $file4 $file5"

trap '{ echo "trapped signal - removing temporary files" ; rm -rf $tmpfiles ; }' 1 2 3 15

# Remove files from prior failed run
rm -rf $tmpfiles

# Remember to quote variables in generated makefiles( $ -> \$ ).
cat > $file1 <<EOT
# Testing parallel execution
SHELL*:=/bin/sh 
SHELLFLAGS*:=-ce

testfile : testfile2 testfile3 testfile1
	+@echo xx > \$@

testfile1 :
	+@echo making \$@ 1>&2
	+@sleep 1
	+@printf t1
	+@echo 1 > \$@

testfile2 :
	+@echo making \$@ 1>&2
	+@sleep 2
	+@printf t2
	+@echo 2 > \$@

testfile3 :
	+@echo making \$@ 1>&2
	+@sleep 3
	+@printf t3
	+@echo 3 > \$@

EOT

output=`eval ${DMAKEPROG} -r -P3 -f $file1`
result=$?

# In parallel operation the targets with the smaller sleep value
# will finish first.
if test "$output" != "t1t2t3"; then
  echo "Wrong result"
  result=1
fi


test $result -eq 0 && echo "Success - Cleaning up" && rm -f ${tmpfiles}
test $result -ne 0 && echo "Failure!"
exit $result
